<?php

class work_Controller extends Controller {
    
    public function _default($params) {
        $view = new work_View(new Module((int)$params['module']));
        if(isset($_POST['ptid'])) {
            $task = new ProjectTask((int)$_POST['ptid']);
            $work = new Work();
            if($work->start(TheUser::getInstance(), $task)) {
                $view->createView('menu',$params);
            } else {
                $view->createView('startWorkError',$params);
            }
        } else {
            $view->createView('menu',$params);
        }
        return $view;
    }
    
    public function endWork($params) {
        $view = new work_View(new Module((int)$params['module']));
        $work = new Work();
        unset($params['ptid']);
        unset($params['wid']);
        unset($params['action']);
        if($work->stop(TheUser::getInstance())) {
            $view->createView('menu',$params);
        } else {
            $view->createView('stopWorkError',$params);
        }
        return $view;
    }
    
    public function endTask($params) {
        $view = new work_View(new Module((int)$params['module']));
        $work = new Work();
        
        $stopped = false;
        if($work->getCurrentWork(TheUser::getInstance())) {
            $stopped = $work->stop(TheUser::getInstance());
        }
        $closed = false;
        $task = new ProjectTask((int)$params['ptid']);
        if(!$task->atProgress()) {
            $closed = $task->closeTask();
        }
        unset($params['ptid']);
        unset($params['wid']);
        unset($params['action']);
        if($stopped && $closed) {
            $view->createView('menu',$params);
        } else {
            $view->createView('closeTaskError',$params);
        }
        
        return $view;
    }
}

?>
